package yanyan.com.tochar;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import androidx.annotation.Nullable;
import yanyan.com.tochar.beans.WebAppInfo;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.DialogUtil;
import yanyan.com.tochar.utils.EmptyUtils;
import yanyan.com.tochar.utils.StringUtil;
import yanyan.com.tochar.utils.ToastUtil;

public class FunsWebAppActivity extends Activity {
    private ProgressDialog progressDialog;
    private WebAppInfo webAppInfo;
    private TextView title;
    private WebView webView;


    public FunsWebAppActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funs_webapp);
        title=findViewById(R.id.bar_title);
        webView=findViewById(R.id.webapp_web);

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.supportMultipleWindows();  //多窗口
        settings.setAllowFileAccess(true);  //设置可以访问文件
        settings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        settings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        //支持内容重新布局
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setAppCacheEnabled(true);//是否使用缓存
//        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        settings.setDomStorageEnabled(true);//DOM Storage
        settings.setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
        settings.setDomStorageEnabled(true);//主要是这句

        webAppInfo= (WebAppInfo) getIntent().getSerializableExtra("webapp");
        if(webAppInfo==null){
            ToastUtil.showShotToast(this,"资源加载失败");
        }else {
            title.setText(webAppInfo.getTitle());
            webView.loadUrl(webAppInfo.getUrl());
        }

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //这里一定要是url  不能是网页
                view.loadUrl(url);
                return true;
            }

            @Nullable
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String  url) {
                if (url.startsWith("http") || url.startsWith("https")) { //http和https协议开头的执行正常的流程
                    return super.shouldInterceptRequest(view, url);
                } else { //其他的URL则会开启一个Acitity然后去调用原生APP
                    if(url.indexOf("mqqwpa")>-1){
                        Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(in);
                    }

                    return null;
                }
            }
            //shouldInterceptRequest

            //证书的设置  支持所有的证书
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

        });

        //加载
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if(newProgress==100 && progressDialog!=null && progressDialog.isShowing()){
                    progressDialog.dismiss();
                }else {
                    if(progressDialog==null) {
                        progressDialog = new ProgressDialog(FunsWebAppActivity.this);
                        progressDialog.setTitle("正在载入");
                        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                        progressDialog.setProgress(newProgress);
                        progressDialog.show();
                    }
                    else {
                        progressDialog.setProgress(newProgress);
                    }
                }
            }
        });

        if(webAppInfo!=null && EmptyUtils.isNotEmpty(webAppInfo.getNotice())){
            DialogUtil dialog=new DialogUtil(this,"提示",webAppInfo.getNotice(),
                    "知道了","知道了",null,null,true);
            dialog.show();

        }
    }

    /**
     * 返回
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== KeyEvent.KEYCODE_BACK  && !webView.getUrl().equals(webAppInfo.getUrl())) {
            if(webView.canGoBack()) {
                webView.goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}
